<?php

namespace app\admin\controller;

use app\admin\model\Admin;
use app\admin\model\User;
use app\common\controller\Backend;
use app\common\model\Attachment;
use app\common\model\recharge\Recharge;
use app\common\model\trends\Trends;
use fast\Date;
use think\Db;

/**
 * 控制台
 *
 * @icon   fa fa-dashboard
 * @remark 用于展示当前系统中的统计数据、统计报表及重要实时数据
 */
class Dashboard extends Backend
{

    /**
     * 查看
     */
    public function index()
    {
        try {
            \think\Db::execute("SET @@sql_mode='';");
        } catch (\Exception $e) {

        }
        $auth_group_access = \think\Db::name('auth_group_access')->where('uid', $this->auth->id)->find();
        if (empty($auth_group_access)) {
            $merchant = [];
        } elseif ($auth_group_access['group_id'] == 2) {
            $merchant = ['merchant_id' => $this->auth->id];
        } else {
            $merchant = [];
        }

        $column = [];
        $starttime = Date::unixtime('day', -6);
        $endtime = Date::unixtime('day', 0, 'end');
        $joinlist = Db("user")->where('jointime', 'between time', [$starttime, $endtime])->where($merchant)
            ->field('jointime, status, COUNT(*) AS nums, DATE_FORMAT(FROM_UNIXTIME(jointime), "%Y-%m-%d") AS join_date')
            ->group('join_date')
            ->select();
        for ($time = $starttime; $time <= $endtime;) {
            $column[] = date("Y-m-d", $time);
            $time += 86400;
        }
        $userlist = array_fill_keys($column, 0);
        foreach ($joinlist as $k => $v) {
            $userlist[$v['join_date']] = $v['nums'];
        }

//        $dbTableList = Db::query("SHOW TABLE STATUS");

//        $addonList = get_addon_list();
//        $totalworkingaddon = 0;
//        $totaladdon = count($addonList);
//        foreach ($addonList as $index => $item) {
//            if ($item['state']) {
//                $totalworkingaddon += 1;
//            }
//        }

        $totalworkingaddon = Trends::where(['status' => 1, 'frame_status' => 1])->where($merchant)->count();

        $totaladmin = $auth_group_access['group_id'] == 2 ? 1 : Admin::count();
        $this->view->assign([

            'totaluser' => User::where($merchant)->count(),
            'totaladmin' => $totaladmin ,
            'todayusersignup' => User::whereTime('jointime', 'today')->where($merchant)->count(),
            'todayuserlogin' => User::whereTime('logintime', 'today')->where($merchant)->count(),
            'sevendau' => User::whereTime('jointime|logintime|prevtime', '-7 days')->where($merchant)->count(),
            'thirtydau' => User::whereTime('jointime|logintime|prevtime', '-30 days')->where($merchant)->count(),
            'threednu' => User::whereTime('jointime', '-3 days')->where($merchant)->count(),
            'sevendnu' => User::whereTime('jointime', '-7 days')->where($merchant)->count(),
//            'dbtablenums' => count($dbTableList),
            'dbtablenums' => Recharge::where($merchant)->sum('money'),

//            'dbsize' => array_sum(array_map(function ($item) {
//                return $item['Data_length'] + $item['Index_length'];
//            }, $dbTableList)),

            'dbsize' => Recharge::where('status', 0)->where($merchant)->sum('money'),


            'totalworkingaddon' => $totalworkingaddon, // 帖子数量

            'attachmentnums' => Attachment::count(),
            'attachmentsize' => Attachment::sum('filesize'),
            'picturenums' => Attachment::where('mimetype', 'like', 'image/%')->count(),
            'picturesize' => Attachment::where('mimetype', 'like', 'image/%')->sum('filesize'),


        ]);

        $country = ['中国', '美国'];
//        $country = ['直接访问', '邮件营销', '联盟广告', '视频广告', '搜索引擎'];

        $country_data = [
            ['value' => 310, 'name' => '中国'],
            ['value' => 234, 'name' => '美国']
        ];


        $this->assignconfig('column', array_keys($userlist));
        $this->assignconfig('userdata', array_values($userlist));

        $this->assignconfig('country', $country);
        $this->assignconfig('country_data', $country_data);

        return $this->view->fetch();
    }

}
